import React, { useMemo, useState, useEffect } from 'react';
import { Title } from 'base/ctl/title';
import { loc } from 'local';
import { g_wsn } from 'db/state';
import { Time } from 'base/ctl/base';
import { uDb, cline, GridTable, useHigh, obj_str, loc_date, is_empty, Wait, save_xlsx, d_sort, NuiBt } from 'sui';
import { get_qr } from './cmd';
import { adjust_val } from '../qr/cmd';

const AnyView = ({ hreturn, cur, title }) => {
    const [main, setMain] = useState();
    const [wait, setWait] = useState(false);
    const [high] = useHigh(130, 200)

    useEffect(() => {
        const sn = obj_str(cur, "sn");
        if (!sn) {
            return;
        }

        uDb(null, "psn/list",
            {
                line: cline(),
                k: sn
            },
            r => {
                d_sort(r, "sn");

                adjust_val(r);

                r.forEach(d => {
                    const qr = get_qr(d);
                    if (!qr) {
                        return;
                    }

                    d["kia"] = obj_str(d, "kia");
                    d["sia"] = obj_str(d, "sia");
                    d["kea"] = obj_str(d, "kea");
                    d["sea"] = obj_str(d, "kea");

                    const tech = obj_str(d, "tech");
                    const tmp = tech.split("-");

                    const a = tmp[0];
                    let b = "";

                    if (tmp.length === 2) {
                        b = tmp[1];
                        if (b.endsWith("N")) {
                            b = b.slice(0, -1);
                        }

                        b = b.padEnd(4);
                    }

                    d["qr"] = `${a}-${b}${qr}`;

                    d["s_angle"] = d["angle"];
                    d["s_outwidth"] = d["outwidth"];
                    d["s_standout"] = d["standout"];
                    d["b_standout"] = d["standout"];
                    d["b_dent"] = d["dent"];
                });

                setMain(r);
            }, e => setMain(null));
    }, [cur]);

    const col = useMemo(() => {
        const tech = obj_str(cur, "tech");
        if (!tech) {
            return null;
        }

        return [
            {name: "型号", key: 'tech', width: 120},
            {name: tech, key: 'k1', width: 100},
            {name: "k2", key: 'k2', width: 100},
            {name: "k3", key: 'k3', width: 100},
            {name: "轴承编码", key: 'sn', width: 100},
            {name: "外圆尺寸", key: 'outdia', width: 100},
            {name: "内孔尺寸", key: 'india', width: 100},
            {name: "宽度", key: 'outwidth', width: 100},
            {name: "b", key: 'standout', width: 100},
            {name: "f", key: 'dent', width: 100},
            {name: "b", key: 'b_standout', width: 100},
            {name: "f", key: 'b_dent', width: 100},
            {name: "接触角尾数", key: 'angle', width: 120},
            {name: "内圈径向跳动", key: 'kia', width: 120},
            {name: "内圈轴向跳动", key: 'sia', width: 120},
            {name: "外圈径向跳动", key: 'kea', width: 120},
            {name: "外圈轴向跳动", key: 'sea', width: 120},
            {name: "接触角标签", key: 's_angle', width: 120},
            {name: "B", key: 's_outwidth', width: 100},
            {name: "凸出量", key: 's_standout', width: 100},
            {name: "判定", key: 'k6', width: 100},
            {name: "二维码", key: 'qr', width: 300},
        ];
    }, [cur]);

    const hout = () => {
        if (is_empty(main)) {
            alert("数据为空");
        }

        setWait(true);

        const head = [];

        const h = col.map(d => {
            head.push(d.key);

            return { value: d.name };
        });

        const row = [h];
        const str_h = ["tech", "sn", "qr", "angle", "kia", "sia", "kea", "sea", "s_angle"];

        const set = new Set();
        str_h.forEach(d => set.add(d));

        main.forEach(d => {
            const tmp = [];

            head.forEach(t => {
                let val = obj_str(d, t);
                if (set.has(t)) {
                    tmp.push({ value: val });
                    return;
                }

                const f = parseFloat(val);
                if (f > 0) {
                    val = `+${val}`;
                }

                tmp.push({ value: val });
            });

            row.push(tmp);
        });

        const tech = obj_str(cur, "tech");

        save_xlsx(row, `${tech}_${title}_${loc_date()}.xlsx`, () => setWait(false));
    };

    return (
        <div className="flex flex-col ml-2 mr-2">
            <div className="inline-flex items-center justify-between mt-2 mb-2">
                <div className="inline-flex">
                    <Title label={loc("base_43")} value={g_wsn()} width="w-56"/>
                    <Title label={title} width="w-56"/>
                </div>
                <Time/>
                <div className="inline-flex">
                    <NuiBt color="green" className="h-16 mr-4" hcmd={hout}>导出</NuiBt>
                    <NuiBt className="h-16 mr-2" hcmd={hreturn}>返回</NuiBt>
                </div>
            </div>
            <GridTable col={col} row={main} high={high}/>
            <Wait wait={wait}/>
        </div>
    );
};

export {AnyView};
